<template>
  <div class="article-edit">
    <el-card class="!border-none" shadow="never">
      <el-page-header :content="$route.meta.title" @back="$router.back()"/>
    </el-card>
    <el-card class="mt-4 !border-none" shadow="never">
      <el-form
          ref="formRef"
          class="ls-form"
          :model="formData"
          label-width="85px"
          :rules="rules"
      >
        <div class="xl:flex">
          <div>
            <el-form-item label="标题"  prop="name">
              <div class="w-80">
                <el-input
                    v-model="formData.name"
                    placeholder="请输入标题"
                    type="text"
                    maxlength="255"
                    show-word-limit
                    clearable
                />
              </div>
            </el-form-item>
            <el-form-item label="简介"   prop="desc">
              <div class="w-80">
                <el-input
                    v-model="formData.desc"
                    placeholder="请输入文章简介"
                    type="textarea"
                    :autosize="{ minRows: 3, maxRows: 6 }"
                    :maxlength="200"
                    show-word-limit
                    clearable
                />
              </div>
            </el-form-item>
            <el-form-item label="banner图" prop="banner_url">
              <div>
                <div>
                  <material-picker v-model="formData.banner_url" :limit="1"/>
                </div>
                <div class="form-tips">建议尺寸：240*180px</div>
              </div>
            </el-form-item>
            <el-form-item label="标题栏颜色" label-width="97px"  prop="color">
              <color-picker
                  class="max-w-[400px]"
                  v-model="formData.color"
                  default-color="#4173ff"
              />
            </el-form-item>

            <el-form-item label="展现形式" required prop="show_type">
              <el-radio-group v-model="formData.show_type">
                <el-radio :label="1">渠道对接</el-radio>
                <el-radio :label="2">产品库</el-radio>
              </el-radio-group>
            </el-form-item>

            <el-form-item label="内容状态" required prop="is_enable">
              <el-radio-group v-model="formData.is_enable">
                <el-radio :label="1">显示</el-radio>
                <el-radio :label="0">隐藏</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="排序" prop="sort">
              <div>
                <el-input-number v-model="formData.sort" :min="2" :max="9999"/>
                <div class="form-tips">大于1， 数值越大越排前</div>
              </div>
            </el-form-item>
          </div>
          <div class="xl:ml-20">
            <el-form-item label="内容" prop="content">
              <editor v-model="formData.content" :height="667" :width="375"/>
            </el-form-item>
          </div>
        </div>
      </el-form>
    </el-card>
    <footer-btns>
      <el-button type="primary" @click="handleSave">保存</el-button>
    </footer-btns>
  </div>
</template>

<script lang="ts" setup name="bannerListsEdit">
import type {FormInstance} from 'element-plus'
import {bannerDetail, bannerEdit, bannerAdd} from '@/api/article'
import useMultipleTabs from '@/hooks/useMultipleTabs'

const route = useRoute()
const router = useRouter()
const formData = reactive({
  id: '',
  name: '',
  is_plat: 1,
  show_type: 0,
  banner_url: '',
  desc: '',
  content: '',
  is_enable: 1,
  color: '',
  sort: 0,
  area_id: ''
})

const {removeTab} = useMultipleTabs()
const formRef = shallowRef<FormInstance>()
const rules = reactive({
  name: [{required: true, message: '标题不能为空', trigger: 'blur'}],
  desc: [{required: true, message: '简介不能为空', trigger: 'blur'}],
  show_type: [{required: true, message: '请选择显示方式', trigger: 'blur'}],
  banner_url: [{required: true, message: '请选择banner图', trigger: 'blur'}],
  is_enable: [{required: true, message: '请选择内容状态', trigger: 'blur'}],
  color: [{required: true, message: '请选择一个颜色', trigger: 'blur'}],
  sort: [{required: true, message: '排序不能为空', trigger: 'blur'}],
})

const getDetails = async () => {
  const data = await bannerDetail({
    id: route.query.id
  })
  Object.keys(formData).forEach((key) => {
    //@ts-ignore
    formData[key] = data[key]
  })
}


const handleSave = async () => {
  await formRef.value?.validate()
  if (route.query.id) {
    await bannerEdit(formData)
  } else {
    await bannerAdd(formData)
  }
  removeTab()
  router.back()
}

route.query.id && getDetails()
</script>
